package com.cssw.fyzb.tps.api.live;

import com.cssw.fyzb.tps.config.live.TxLive;
import com.cssw.fyzb.tps.model.vo.CreateLiveStreamVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Feng Chen
 */
@Function(description = "创建直播流", parameters = {
        @Parameter(name = "app", type = Type.String, description = "推流路径", required = true),
        @Parameter(name = "stream", type = Type.String, description = "流名称", required = true),
        @Parameter(name = "hour", type = Type.Integer, description = "直播时长(单位小时，默认24小时)", required = false),
}, result = @Result(properties = {
        @Property(name = "pushUrl", type = Type.String, description = "推流地址"),
        @Property(name = "pullUrl", type = Type.String, description = "拉流地址"),
}))
@Component
public class CreateLiveStream implements Action {

    @Autowired
    private TxLive txLive;

    private static final Logger logger = LoggerFactory.getLogger(CreateLiveStream.class);

    @Override
    public Response execute(Request request) throws Exception {
        String app = request.getString("app");
        String stream = request.getString("stream");
        Integer hour = request.getInteger("hour");
        try {
            CreateLiveStreamVo vo = txLive.createLiveStream(app, stream, hour);
            return Response.success().put("data", vo);
        } catch (Exception e) {
            logger.error("", e);
        }
        return Response.fail("操作异常");
    }


}
